<?php
if( !defined('CORE') ) exit('Request Error!');
/**
 * 后台管理控制器
 *
 * @version $Id$
 */
class ctl_task
{

    public function __construct()
    {
        tpl::assign('ns', time());
    }

    /**
     * 主入口
     */
    public function index()
    {
        $keyword = req::item('keyword', '');
        $where = array();
        if (!empty($keyword)) 
        {
            $where[] = "(`name` LIKE '%{$keyword}%' OR `filename` LIKE '%{$keyword}%')";
        }
        $where = empty($where) ? '' : 'WHERE ' . implode(' AND ', $where);

        $sql = "SELECT * FROM `users_task` {$where} ORDER BY `oid` ASC";
        $list = db::get_all($sql);

        tpl::assign('list', $list);
        tpl::display('task.index.tpl');
        exit();
    }

    public function add()
    {
        if (req::$posts) 
        {
            db::insert('users_task', req::$posts);
            $this->save();
            $gourl = '?ct=task&ac=index';
            mod_msgbox::show("任务管理", "添加成功", $gourl);
        }
        else 
        {
            tpl::display('task.add.tpl');
        }
    }


    public function edit()
    {
        $id = req::item('id', 0);
        if (req::$posts) 
        {
            db::update('users_task', req::$posts, "`id`={$id}");
            $this->save();
            $gourl = '?ct=task&ac=index';
            mod_msgbox::show("任务管理", "修改成功", $gourl);
        }
        else 
        {
            $sql = "SELECT * FROM `users_task` WHERE `id`={$id}";
            $data = db::get_one($sql);
            tpl::assign('v', $data);
            tpl::display('task.edit.tpl');
        }
    }


    public function del()
    {
        $id = req::item('id', 0);
        if (empty($id)) 
        {
            exit('请选择要删除的任务');
        }
        $sql = "DELETE FROM `users_task` WHERE `id`={$id}";
        db::query($sql);
        $this->save();
        $gourl = '?ct=task&ac=index';
        mod_msgbox::show("任务管理", "删除成功", $gourl);
    }

    public function batch_edit()
    {
        $ids = req::item('ids', array());
        $oids = req::item('oids', array());
        if (empty($ids)) 
        {
            mod_msgbox::show("任务管理", "请选择要修改的任务", -1);
        }
        foreach ($ids as $id) 
        {
            $sql = "UPDATE `users_task` SET `oid`={$oids[$id]} WHERE `id`={$id}";
            db::query($sql);
        }
        $this->save();
        $gourl = '?ct=task&ac=index';
        mod_msgbox::show("任务管理", "批量修改成功", $gourl);
    }

    /**
     * 保存任务列表到文件，以备crond调用 
     * 
     * @access public
     * @return void
     */
    public function save()
    {
        $sql = "SELECT * FROM `users_task` ORDER BY `oid` ASC";
        $list = db::get_all($sql, '' , true);
        if ($list) 
        {
            $str = '<?php' . "\n";
            $str .= '$task_list = array(' . "\n";
            foreach ($list as $v) 
            {
                $filename = explode("\n", $v['filename']);
                $filename = "'" . implode("','", $filename) . "'";
                $filename = str_replace(array("\n", "\r"), "", $filename);
                $str .= !$v['status'] ? '//  ' : '    ';
                $str .= "'{$v['runtime_format']}' => array({$filename}),//{$v['name']}\n";
            }
            $str .= ");";
            util::put_file(PATH_DATA . '/task_list.php', $str);
        }
        else 
        {
            unlink(PATH_CROND. '/task_list.php');
        }
    }
}
